package io.renren.modules.substation.controller;

import java.text.SimpleDateFormat;
import java.util.*;

import io.renren.modules.substation.entity.*;
import io.renren.modules.substation.service.TBaseDictInfoService;
import io.renren.modules.substation.service.TBusiDevStatusInfoService;
import io.renren.modules.substation.service.TInfoAreaInfoService;
import io.renren.modules.sys.controller.AbstractController;
import io.renren.modules.sys.entity.SysMenuEntity;
import io.renren.modules.sys.entity.SysRoleEntity;
import io.renren.modules.sys.service.SysUserRoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.substation.service.TInfoDevInfoService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 监测设备信息表
 *
 * @author yzx
 * @email sunlightcs@gmail.com
 * @date 2020-06-13 10:15:11
 */
@RestController
@RequestMapping("substation/tinfodevinfo")
public class TInfoDevInfoController extends AbstractController {
    @Autowired
    private TInfoDevInfoService tInfoDevInfoService;

    @Autowired
    private TBaseDictInfoService tBaseDictInfoService;

    @Autowired
    private TInfoAreaInfoService tInfoAreaInfoService;


    @Autowired
    private TBusiDevStatusInfoService tBusiDevStatusInfoService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("substation:tinfodevinfo:list")
    public R list(@RequestParam Map<String, Object> params){

        PageUtils page = tInfoDevInfoService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 设备类型
     */
    @GetMapping("/devTypelist")
    //@RequiresPermissions("substation:tinfodevinfo:devTypelist")
    public R devTypelist(){
        logger.debug("getdevTypelist start ");
        List<SelectItemInfo> retList = new ArrayList<SelectItemInfo>();
        SelectItemInfo temp0=new SelectItemInfo("--请选择--","");
        retList.add(temp0);

        List<TBaseDictInfoEntity> dic_List=tBaseDictInfoService.queryByDictKey("DEV_TYPE");
        for(int i=0;i<dic_List.size();i++)
        {
            SelectItemInfo temp=new SelectItemInfo(dic_List.get(i).getDictDesc(),dic_List.get(i).getDictValue());
            retList.add(temp);
        }

//        //00-局放监测设备;01-动力环境监测;02-其他'
//        SelectItemInfo temp0=new SelectItemInfo("-请选择-","");
//        SelectItemInfo temp1=new SelectItemInfo("00-局放监测设备","00");
//        SelectItemInfo temp2=new SelectItemInfo("01-动力环境监测","01");
//        SelectItemInfo temp3=new SelectItemInfo("02-其他","02");
//
//        retList.add(temp1);
//        retList.add(temp2);
//        retList.add(temp3);

        logger.debug("getdevTypelist end ");

        return R.ok().put("list", retList);
    }


    /**select
     * 选择菜单(添加、修改菜单)
     */
    @RequestMapping("/devAreaList")
    public R devAreaList(){

        logger.debug("devArealist start ");
        //查询列表数据
        List<TInfoAreaInfoEntity> areaList = tInfoAreaInfoService.list();

        List<SelectItemInfo> devAreaList = new ArrayList<SelectItemInfo>();
        for(TInfoAreaInfoEntity o : areaList){
            logger.debug(o.getAreaName());
            SelectItemInfo m = new SelectItemInfo(o.getAreaName(),o.getAreaId().toString());
            devAreaList.add(m);
        }
        logger.debug("devArealist end ");
        return R.ok().put("list", devAreaList);
    }


    /**
     *
     */
    @RequestMapping("/getDevListByArea")
   // @RequiresPermissions("substation:tinfodevinfo:getDevListByArea")
    public R getDevListByArea(@RequestParam Long areaId){
        List<TInfoDevInfoEntity> devList = tInfoDevInfoService.getDevListByArea(areaId);
        List<SelectItemInfo> list = new ArrayList<>();
        for(int i=0;i<devList.size();i++){
            SelectItemInfo temp=new SelectItemInfo(devList.get(i).getDevName(),devList.get(i).getDevCode());
            list.add(temp);
        }
        return R.ok().put("list", list);
    }

    @RequestMapping("/getAllDevMap")
    public R getAllDevMap(){
        List<TInfoDevInfoEntity> devList = tInfoDevInfoService.list();
        Map<String, Object> map = new HashMap<>();
        for(TInfoDevInfoEntity o : devList){
            map.put(o.getDevCode(), o.getDevType());
        }
        return R.ok().put("map", map);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("substation:tinfodevinfo:info")
    public R info(@PathVariable("id") Long id){
		TInfoDevInfoEntity tInfoDevInfo = tInfoDevInfoService.getById(id);

        return R.ok().put("tInfoDevInfo", tInfoDevInfo);
    }



    public Boolean checkIsExist(String devCode){
        TInfoDevInfoEntity devQuery= new TInfoDevInfoEntity();
        devQuery.setDevCode(devCode);
        List<TInfoDevInfoEntity> devList=tInfoDevInfoService.queryDevListByCondition(devQuery);
        if(devList.size()>0)
        {
            logger.debug("该设备已存在");
            return true;
        }
        else
        {
            logger.debug("该设备不存在");
            return  false;
        }

    }
    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("substation:tinfodevinfo:save")
    public R save(@RequestBody TInfoDevInfoEntity tInfoDevInfo){


        if(checkIsExist(tInfoDevInfo.getDevCode()))
        {
            logger.debug("该设备已存在");
            return R.error("该设备已存在");
        }

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String date = df.format(new Date());// new Date()为获取当前系统时间，也可使用当前时间戳


        tInfoDevInfo.setCreateOpr(getUserId());
        tInfoDevInfo.setLastOpr(getUserId());
        tInfoDevInfo.setLastTime(new Date());
        tInfoDevInfo.setCreateTime(new Date());

		tInfoDevInfoService.save(tInfoDevInfo);

        TBusiDevStatusInfoEntity  tBusiDevStatusInfoEntity =new TBusiDevStatusInfoEntity();
        tBusiDevStatusInfoEntity.setDevCode(tInfoDevInfo.getDevCode());
        tBusiDevStatusInfoEntity.setIsVaild(tInfoDevInfo.getIsVaild());
        tBusiDevStatusInfoEntity.setDevStatus("0");
        tBusiDevStatusInfoEntity.setIsAlarm("0");
        tBusiDevStatusInfoEntity.setCreateOpr(getUserId());
        tBusiDevStatusInfoEntity.setLastOpr(getUserId());
        tBusiDevStatusInfoEntity.setLastTime(new Date());
        tBusiDevStatusInfoEntity.setCreateTime(new Date());

        tBusiDevStatusInfoService.save(tBusiDevStatusInfoEntity);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("substation:tinfodevinfo:update")
    public R update(@RequestBody TInfoDevInfoEntity tInfoDevInfo){


        tInfoDevInfo.setLastOpr(getUserId());
        tInfoDevInfo.setLastTime(new Date());
		tInfoDevInfoService.updateById(tInfoDevInfo);


        TBusiDevStatusInfoEntity  tBusiDevStatusInfoEntity =new TBusiDevStatusInfoEntity();
        tBusiDevStatusInfoEntity.setDevCode(tInfoDevInfo.getDevCode());
        tBusiDevStatusInfoEntity.setIsVaild(tInfoDevInfo.getIsVaild());
        tBusiDevStatusInfoEntity.setLastOpr(getUserId());
        tBusiDevStatusInfoEntity.setLastTime(new Date());

        tBusiDevStatusInfoService.updateByCode(tBusiDevStatusInfoEntity);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("substation:tinfodevinfo:delete")
    public R delete(@RequestBody Long[] ids){


		for(int i=0;i<ids.length;i++)
        {
            Long id=ids[i];
            TInfoDevInfoEntity tInfoDevInfo = tInfoDevInfoService.getById(id);
            Map<String,Object> delMap= new HashMap<String,Object>();
            delMap.put("DEV_CODE",tInfoDevInfo.getDevCode());
            tBusiDevStatusInfoService.removeByMap(delMap);
        }
        tInfoDevInfoService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

    /**
     * 获取无所属区域的设备列表
     */
    @RequestMapping("/devlistnoarea")
    @RequiresPermissions("substation:tinfodevinfo:devlistnoarea")
    public R devlistnoarea(){

        List<TInfoAreaInfoEntity> devNoAreaList = tInfoDevInfoService.queryNoAreaList();

        return R.ok().put("devNoAreaList", devNoAreaList);
    }

    /**
     * 批量修改设备所属区域-由区域页面发起的配置
     */
    @RequestMapping("/update_devarea_byarea")
    @RequiresPermissions("substation:tinfodevinfo:update_devarea_byarea")
    public R updateDevAreaByArea(@RequestBody Map<String, Object> params){
        Long areaId = Long.valueOf(params.get("areaId").toString());
        ArrayList<Integer> devList = (ArrayList<Integer>)params.get("devList");

        for (Integer devId : devList){
            TInfoDevInfoEntity o = new TInfoDevInfoEntity();
            o.setId(Long.valueOf(devId));
            o.setDevArea(areaId);
            o.setLastOpr(getUserId());
            o.setLastTime(new Date());
            tInfoDevInfoService.updateById(o);
        }

        return R.ok();
    }

    /**
     * 批量清除设备所属区域-由区域页面发起的配置
     */
    @RequestMapping("/clear_devarea_byarea")
    @RequiresPermissions("substation:tinfodevinfo:clear_devarea_byarea")
    public R clearDevAreaByArea(@RequestBody Map<String, Object> params){
        ArrayList<Integer> devList = (ArrayList<Integer>)params.get("devList");

        for (Integer devId : devList){
            TInfoDevInfoEntity o = new TInfoDevInfoEntity();
            o.setId(Long.valueOf(devId));
            o.setDevArea(null);
            o.setLastOpr(getUserId());
            o.setLastTime(new Date());
            tInfoDevInfoService.updateAreaNullById(o);
        }

        return R.ok();
    }


}
